﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using IniFiles;

namespace Cali
{
    public class IniConfig
    {
        public string _sPath { set; get; }
        public string[] _sTestNames { set; get; }
        public string _sModelName { set; get; }
        public string _sBomNumber { set; get; }
        public string _sFRUBom { set; get; }
        public string _sCaliBom { set; get; }
        public string _sAddressPSU { set; get; }
        public string _sAddressEEP { set; get; }
        public string _sAddressPSU2 { set; get; }
        public string _sAddressEEP2 { set; get; }
        
        public string[] _sSources { set; get; }
        public string[] _sPowerMeters { set; get; }
        public string[] _sELoads { set; get; }

        public string _sTypeI2CIO { set; get; }
        public string _sPassword { set; get; }
        public bool _bAutoTestEn { set; get; }
        public bool _bAutoExit { set; get; }
        public bool _bDual { set; get; }
        public bool _bReadSN { set; get; }
        public bool _bSNInput { set; get; }
        public bool _b9E { set; get; }
        public bool _bSN { set; get; }

        public string[] _sOutput { set; get; }
        public int _nLoad_No { set; get; }
        public int _nVLoad_No { set; get; }
        public string[] _sLoad_Indexs { set; get; }
        public string[] _sLoad_Chan { set; get; }
        public string[] _sLoad_Type { set; get; }

        public string[] _sLoad_Indexs2 { set; get; }
        public string[] _sLoad_Chan2 { set; get; }
        public string[] _sLoad_Type2 { set; get; }

        public string[] _sCurr1 { set; get; }
        public string[] _sCurr2 { set; get; }
        public string[] _sCurr3 { set; get; }
        public string[] _sCurr4 { set; get; }
        public string[] _sCurr5 { set; get; }

        public string[] _sItemsTest { set; get; }

        public string[] _sVmax { set; get; }
        public string[] _sVmax2 { set; get; }
        public string[] _sVmin { set; get; }
        public string[] _sVmin2 { set; get; }

        public bool _bMESEnable { set; get; }
        public string _Mes_ServerID { set; get; }
        public string _Mes_Account { set; get; }
        public string _Mes_Catlog { set; get; }
        public string _Mes_PWD { set; get; }
        public string _Mes_ResName { set; get; }
        public string _Mes_StationName { set; get; }
        public string _Mes_UserName { set; get; }
        public int _Mes_Number { set; get; }

        public string _sVin { set; get; }
        public string _sFC { set; get; }
        public int _nVinScale { set; get; }
        public int _nIinScale { set; get; }
        public int _nPinScale { set; get; }
        public int _nVoutScale { set; get; }
        public int _nIoutScale { set; get; }
        public int _nVsbScale { set; get; }
        public int _nIsbScale { set; get; }

        public string _sVR_SetPoint1 { set; get; }
        public string _sVR_SetPoint1Min { set; get; }
        public string _sVR_SetPoint1Max { set; get; }
        public string _sVR_SetPoint2 { set; get; }
        public string _sVR_SetPoint2Min { set; get; }
        public string _sVR_SetPoint2Max { set; get; }

        public string _sVR_SetTarget { set; get; }
        public string _sVR_TargetMin { set; get; }
        public string _sVR_TargetMax { set; get; }

        public string _sIbusVoltFull { set; get; }
        public string _sIbusCurrFull { set; get; }

        public string _sIbusRatio1 { set; get; }
        public string _sIbusRatio2 { set; get; }

        public string _sVRCali1 { set; get; }
        public string _sVRCali2 { set; get; }
        public string _sVRCali3 { set; get; }

        //NumberTest
        public string _sReadDelayTime { set; get; }
        public string _sMeasDelayTime { set; get; }
        public int _nReadADCNo { set; get; }
        public int[] _nPages { set; get; }
        public string[] _sReadADCType { set; get; }
        public string[] _sReadADCAddress { set; get; }
        public string[] _sPercentageMax { set; get; }
        public string[] _sPercentageMin { set; get; }
        public string _sPinCheckPoint { set; get; }
        public string _sPoutCheckPoint { set; get; }
        public string _sIinCheckPoint { set; get; }
        public string _sIoutCheckPoint { set; get; }


        //public string _sIbusCali21 { set; get; }
        //public string _sIbusCali22 { set; get; }
        //public string _sIbusCali23 { set; get; }

        public IniConfig(string Path)
        {
            _sPath = Path;
            _sSources = new string[2];
            _sELoads = new string[4];
            _sPowerMeters = new string[2];
        }

        public void ReadGerneralConfig()
        {
            string sBuff;

            if (!File.Exists(_sPath))
                return;

            IniFile file = new IniFile(_sPath);
            _sModelName = file.ReadString("General", "Model Name", "XXXX-AC");
            _sTypeI2CIO = file.ReadString("General", "I2C_Interface", "MCP");
            _bDual = file.ReadInteger("General", "DualTestEnable", 0) == 1;
            _sAddressPSU = file.ReadString("CAL", "uP_Address", "B0");
            _sAddressEEP = file.ReadString("FRU", "eep_Address", "A0");
            _sAddressPSU2 = file.ReadString("CAL", "uP_Address2", "B2");
            _sAddressEEP2 = file.ReadString("FRU", "eep_Address2", "A2");
            _bAutoTestEn = file.ReadInteger("General", "AutoTestEN", 0).Equals(1);
            _bAutoExit = file.ReadInteger("CAL", "AutoExit", 0).Equals(1);
            _b9E = file.ReadInteger("CAL", "9E_Write", 0).Equals(1);
            _bSN = file.ReadInteger("CAL", "SN_Write", 0).Equals(1);
            _bSNInput = file.ReadInteger("General", "SN INPUT", 0).Equals(1);
            _bReadSN = file.ReadInteger("General", "ReadSnFromEEP", 0).Equals(1);
            _bMESEnable = file.ReadInteger("General", "MesEnable", 0).Equals(1);
            _sBomNumber = file.ReadString("General", "BomNumber", "990000");
            _sFRUBom = file.ReadString("General", "FRUBom", "50100000");
            _sCaliBom = file.ReadString("General", "CaliBom", "50100000");

            //_sTestName = file.ReadString("CAL", "Test_Name", "AC Calibration");

            _sSources[0] = file.ReadString("General", "Source Address", "GPIB0::30::INSTR");
            _sSources[1] = file.ReadString("General", "Source Address2", "GPIB::28::INSTR");
            _sPowerMeters[0] = file.ReadString("General", "Power Meter Address", "GPIB0::8::INSTR");
            _sPowerMeters[1] = file.ReadString("General", "Power Meter Address2", "GPIB0::0::INSTR");
            _sELoads[0] = file.ReadString("General", "EL1 Add", "");
            _sELoads[1] = file.ReadString("General", "EL2 Add", "");
            _sELoads[2] = file.ReadString("General", "EL3 Add", "");
            _sELoads[3] = file.ReadString("General", "EL4 Add", "");
            _nLoad_No = file.ReadInteger("General", "Load_No", 0);
            sBuff = file.ReadString("General", "Output", "12V, 12Vsb");
            _sOutput = sBuff.Split(',');
            _nVLoad_No = file.ReadInteger("General", "V_Load_No", 0);

            sBuff = file.ReadString("General", "Load_Index", "4,5,5");
            _sLoad_Indexs = sBuff.Split(',');
            sBuff = file.ReadString("General", "Load_Chan", "7,1,7");
            _sLoad_Chan = sBuff.Split(',');
            sBuff = file.ReadString("General", "Load_Type", "1,0,0");
            _sLoad_Type = sBuff.Split(',');

            sBuff = file.ReadString("General", "Load_Index2", "6,1,1");
            _sLoad_Indexs2 = sBuff.Split(',');
            sBuff = file.ReadString("General", "Load_Chan2", "7,1,7");
            _sLoad_Chan2 = sBuff.Split(','); 
            sBuff = file.ReadString("General", "Load_Type2", "1,0,0");
            _sLoad_Type2 = sBuff.Split(',');

            _sPassword = file.ReadString("General", "Pass Word", "1111");

            sBuff = file.ReadString("0", "I2C1 Test Item", "CAL,64");
            _sItemsTest = sBuff.Split(',');
            _sTestNames = sBuff.Split(',');

            for (int i = 0; i < _sItemsTest.Length; i++)
                _sTestNames[i] = file.ReadString(_sItemsTest[i], "Test_Name", "xxx");

        }

        public void SaveGeneralConfig()
        {
            string sBuff = string.Empty;

            IniFile file = new IniFile(_sPath);

            file.WriteString("General", "Source Address", _sSources[0]);
            file.WriteString("General", "Source Address2", _sSources[1]);
            file.WriteString("General", "Power Meter Address", _sPowerMeters[0]);
            file.WriteString("General", "Power Meter Address2", _sPowerMeters[1]);

            file.WriteString("General", "EL1 Add", _sELoads[0]);
            file.WriteString("General", "EL2 Add", _sELoads[1]);
            file.WriteString("General", "EL3 Add", _sELoads[2]);
            file.WriteString("General", "EL4 Add", _sELoads[3]);

            file.WriteInteger("General", "Load_No", _nLoad_No);
            file.WriteInteger("General", "V_Load_No", _nVLoad_No);
            file.WriteString("General", "Output", stringCombine(_sOutput));
            file.WriteString("General", "Load_Index", stringCombine(_sLoad_Indexs));
            file.WriteString("General", "Load_Chan", stringCombine(_sLoad_Chan));
            file.ReadString("General", "Load_Type", stringCombine(_sLoad_Type));

            file.WriteString("General", "Load_Index2", stringCombine(_sLoad_Indexs2));
            file.WriteString("General", "Load_Chan2", stringCombine(_sLoad_Chan2));
            file.WriteString("General", "Load_Type2", stringCombine(_sLoad_Type2));

            file.WriteString("General", "I2C_Interface", _sTypeI2CIO);
        }

        public string stringCombine(string[] sts)
        {
            string str = string.Empty;
            for (int i = 0; i < sts.Length; i++)
                str += sts[i].Trim() + ',';
            return str.TrimEnd(',');
        }

        public void ReadVRConfig()
        {
            if (!File.Exists(_sPath))
                return;

            IniFile file = new IniFile(_sPath);
            //_sTestName = file.ReadString("VR", "Test_Name", "VR_Adjust");
            _sVin = file.ReadString("VR", "Vin", "220.0");
            _sFC = file.ReadString("VR", "Fin", "50");
            _sVRCali1 = file.ReadString("VR", "VRCali1", "10.83,0.5");
            _sVRCali2 = file.ReadString("VR", "VRCali2", "10.83,0.5");
            _sVRCali3 = file.ReadString("VR", "VRCali3", "21.66,1.0");

            _sIbusCurrFull = file.ReadString("VR", "IBusVoltAtFullLoad", "6.0");
            _sIbusVoltFull = file.ReadString("VR", "IbusFullCurr", "21.67");
            _sIbusRatio1 = file.ReadString("VR", "IBusSetRatio2", "1.0");
            _sIbusRatio2 = file.ReadString("VR", "IBusSetRatio1", "0.5");

            _sVR_SetTarget = file.ReadString("VR", "VoutSetTarget", "12.0");
            _sVR_TargetMin = file.ReadString("VR", "VoutSetErrorMin", "11.9");
            _sVR_TargetMax = file.ReadString("VR", "VoutSetErrorMax", "12.1");

            _sVR_SetPoint1 = file.ReadString("VR", "VoutSetPoint1", "12.1");
            _sVR_SetPoint1Min = file.ReadString("VR", "VoutSetPoint1Min", "12.0");
            _sVR_SetPoint1Max = file.ReadString("VR", "VoutSetPoint1Max", "12.2");

            _sVR_SetPoint2 = file.ReadString("VR", "VoutSetPoint2", "12.4");
            _sVR_SetPoint2Min = file.ReadString("VR", "VoutSetPoint2Min", "12.3");
            _sVR_SetPoint2Max = file.ReadString("VR", "VoutSetPoint2Max", "12.5");
        }

        public void ReadCALConfig()
        {
            string sbuff;
            float fBuf;
            string[] ArrStr;
            Calibration CAL = Form_Main.AC_Cali;

            IniFile file = new IniFile(_sPath);
            //_sTestName = file.ReadString("CAL", "Test_Name", "AC Calibration");
            _sModelName = file.ReadString("CAL", "ModelName", "C1A-C0260-E");
            CAL._sTest_name = _sModelName; 
            _sVin = file.ReadString("CAL", "Vin", "110.0, 220.0");
            ArrStr = _sVin.Split(',');
            for (int i = 0; i < ArrStr.Length; i++)
            {
                CAL._fVinSet[i] = ArrStr[i].Trim();
            }
            _sFC = file.ReadString("CAL", "Fin", "60.0,50.0");
            ArrStr = _sFC.Split(',');
            for (int i = 0; i < ArrStr.Length; i++)
            {
                CAL._fFCinSet[i] = ArrStr[i].Trim();
            }

            sbuff = file.ReadString("CAL", "Curr1", "Cali1");
            sbuff = file.ReadString("Curr", sbuff, "1.083,0.60,0.0");
            _sCurr1 = sbuff.Split(',');

            sbuff = file.ReadString("CAL", "Curr2", "Cali2");
            sbuff = file.ReadString("Curr", sbuff, "");
            _sCurr2 = sbuff.Split(',');

            sbuff = file.ReadString("CAL", "Curr3", "Cali3");
            sbuff = file.ReadString("Curr", sbuff, "");
            _sCurr3 = sbuff.Split(',');

            sbuff = file.ReadString("CAL", "Curr4", "Cali4");
            sbuff = file.ReadString("Curr", sbuff, "");
            _sCurr4 = sbuff.Split(',');

            sbuff = file.ReadString("CAL", "Curr5", "Cali5");
            sbuff = file.ReadString("Curr", sbuff, "");
            _sCurr5 = sbuff.Split(',');

            _nVinScale = file.ReadInteger("CAL", "VinScale", 32);
            _nIinScale = file.ReadInteger("CAL", "IinScale", 256);
            _nPinScale = file.ReadInteger("CAL", "PinScale", 16);
            _nVoutScale = file.ReadInteger("CAL", "VoutScale", 512);
            _nIoutScale = file.ReadInteger("CAL", "IoutScale", 256);
            _nVsbScale = file.ReadInteger("CAL", "VsbScale", 512);
            _nIsbScale = file.ReadInteger("CAL", "IsbScale", 256);
        }

        public void ReadNumberConfig(string sNumber)
        {
            string sBuff;
            IniFile file = new IniFile(_sPath);
            _sVin = file.ReadString(sNumber, "Vin", "0");
            _sFC = file.ReadString(sNumber, "Fin", "0");
            _sAddressPSU = file.ReadString(sNumber, "uP_Address", "B0");
            _sAddressPSU2 = file.ReadString(sNumber, "uP_Address2", "B2");
            _sReadDelayTime = file.ReadString(sNumber, "Read_Delay_Time", "1000");
            _sMeasDelayTime = file.ReadString(sNumber, "Meas_Delay_time", "1000");
            _nReadADCNo = file.ReadInteger(sNumber, "Read_ADC_NO", 8);
            sBuff = file.ReadString(sNumber, "Read_ADC_Type", "");
            _sReadADCType = sBuff.Split(',');
            sBuff = file.ReadString(sNumber, "Read_ADC_Address", "");
            _sReadADCAddress = sBuff.Split(',');
            sBuff = file.ReadString(sNumber, "Percentage_Max", "");
            _sPercentageMax = sBuff.Split(',');
            sBuff = file.ReadString(sNumber, "Percentage_Min", "");
            _sPercentageMin = sBuff.Split(',');
            _sPinCheckPoint = file.ReadString(sNumber, "Pin_Check_Point", "");
            _sPoutCheckPoint = file.ReadString(sNumber, "Pout_Check_Point", "");
            _sIinCheckPoint = file.ReadString(sNumber, "Iin_Check_Point", "");
            _sIoutCheckPoint = file.ReadString(sNumber, "Iout_Check_Point", "");
            sBuff = file.ReadString(sNumber, "Curr", "Load1");
            sBuff = file.ReadString("Curr", sBuff, "");
            _sCurr1 = sBuff.Split(',');
            sBuff = file.ReadString(sNumber, "Curr2", "Load2");
            sBuff = file.ReadString("Curr", sBuff, "");
            _sCurr2 = sBuff.Split(',');
            sBuff = file.ReadString(sNumber, "Vmax", "Vmax1");
            sBuff = file.ReadString("VRange", sBuff, "");
            _sVmax = sBuff.Split(',');
            sBuff = file.ReadString(sNumber, "Vmax2", "Vmax1");
            sBuff = file.ReadString("VRange", sBuff, "");
            _sVmax2 = sBuff.Split(',');
            sBuff = file.ReadString(sNumber, "Vmin", "Vmin1");
            sBuff = file.ReadString("VRange", sBuff, "");
            _sVmin = sBuff.Split(',');
            sBuff = file.ReadString(sNumber, "Vmin2", "Vmin1");
            sBuff = file.ReadString("VRange", sBuff, "");
            _sVmin2 = sBuff.Split(',');
        }
    }
}
